package org.ishequ.miduo.weibo;

import org.ishequ.miduo.R;
import org.ishequ.weibo.utils.http.AsyncImageLoader;
import org.ishequ.weibo.utils.zoom.ImageZoomView;
import org.ishequ.weibo.utils.zoom.SimpleZoomListener;
import org.ishequ.weibo.utils.zoom.ZoomState;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

public class WeiboImageActivity extends BaseActivity{
	
    private ImageZoomView mZoomView;
    private ZoomState mZoomState;
    private Bitmap mBitmap;
    private SimpleZoomListener mZoomListener;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.weibo_image, null);
		root.addView(layout);
		
		// 设置标题栏文字
		titlebarText.setText("浏览微博");
		
		Intent i=this.getIntent();
        if(i!=null){
            Bundle b=i.getExtras();
            if(b!=null){
                if(b.containsKey("url")){
                    String url = b.getString("url");
                    mZoomView =(ImageZoomView)layout.findViewById(R.id.pic);
                    Drawable img = AsyncImageLoader.loadImageFromUrl(url);
                    mZoomState = new ZoomState();
                    mBitmap = drawableToBitmap(img);
                    mZoomView.setImage(mBitmap);
                    mZoomListener = new SimpleZoomListener();
                    mZoomListener.setZoomState(mZoomState);
                    mZoomView.setZoomState(mZoomState);
                    mZoomView.setOnTouchListener(mZoomListener);
                    resetZoomState();
                }
            }
        }
	

	    ZoomControls zoomCtrl = (ZoomControls) layout.findViewById(R.id.zoomCtrl);  
	    zoomCtrl.setOnZoomInClickListener(new OnClickListener(){  
			    @Override  
			    public void onClick(View view) {  
			    float z= mZoomState.getZoom()+0.25f;  
			    mZoomState.setZoom(z);  
			    mZoomState.notifyObservers();  
		    }  
	    });  
	    
	    zoomCtrl.setOnZoomOutClickListener(new OnClickListener(){  
			    @Override  
			    public void onClick(View v) {  
			    float z= mZoomState.getZoom()-0.25f;  
			    mZoomState.setZoom(z);  
			    mZoomState.notifyObservers();  
		    }  
	    });   
	}
	
	private void resetZoomState() {  
		mZoomState.setPanX(0.5f);  
		mZoomState.setPanY(0.5f);  
		  
		final int mWidth = mBitmap.getWidth();  
		final int vWidth= mZoomView.getWidth();  
		Log.i("iw:",vWidth+"");  
		mZoomState.setZoom(1f);  
		mZoomState.notifyObservers();  
		  
	}  
		  
	public Bitmap drawableToBitmap(Drawable drawable) {  
		Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();   
		return bitmap;  
	}

}
